<template>
  <div class="report-form-content">
    <div class="tabs-list-head tabs-list-head-2">
      <span>初始合同信息</span>
    </div>
    <el-divider border-style="dashed" />
    <el-tabs v-model="activeName" class="demo-tabs">
      <el-tab-pane label="基本信息录入" name="1"></el-tab-pane>
      <el-tab-pane label="验收条件" name="2" v-if="obj.needInspection === '1'"></el-tab-pane>
      <el-tab-pane label="收付信息" name="3"></el-tab-pane>
      <el-tab-pane label="附件上传" name="4"></el-tab-pane>
    </el-tabs>
    <div class="tabs-list" v-show="activeName === '1'">
      <el-row :gutter="20">
        <div class="tabs-list-head">
          <span>基本信息</span>
        </div>
        <el-col :sm="24" :lg="24" style="padding-left: 20px">
          <el-card class="update-log">
            <div class="report-form-list-content">
              <div class="report-list">
                <label>合同编号：</label>
                <span>{{ obj.code }}</span>
              </div>
              <div class="report-list">
                <label>合同名称：</label>
                <span>{{ obj.name }}</span>
              </div>
              <div class="report-list">
                <label>签订人：</label>
                <span>{{ showIdLabel(contractSignerList, obj.contractSignerId, 'name') }}</span>
              </div>
              <div class="report-list">
                <label>合同类型：</label>
                <span>{{ showIdLabel(contractTypeList, obj.type, 'name') }}</span>
              </div>
              <div class="report-list">
                <label>负责人：</label>
                <span>{{ showIdLabel(userList, obj.userId, 'name') }}</span>
              </div>
              <div class="report-list">
                <label>合同金额：</label>
                <span>{{ obj.amount }}</span>
              </div>
              <div class="report-list">
                <label>合同起始时间：</label>
                <span>{{ obj.contractLaunch }}</span>
              </div>
              <div class="report-list">
                <label>合同终止时间：</label>
                <span>{{ obj.contractComplete }}</span>
              </div>
              <div class="report-list">
                <label>是否需要验收：</label>
                <span>
                  <EnumTag :enum-list="getEnumList('CommonYesNo')" :enum-key="obj.needInspection" />
                </span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <div class="tabs-list-head" v-if="partyAList.length > 0">
          <span>甲方信息</span>
        </div>
        <el-col :sm="24" :lg="24" style="padding-left: 20px">
          <el-card class="update-log" v-for="(item,index) in partyAList" :key="index">
            <div class="report-form-list-content">
              <div class="report-list">
                <label>参与方类型：</label>
                <span>
                  <DictTag :dict-list="getDictList('ContractParticipationUserType')" :dict-key="item.participationUserType" />
                </span>
              </div>
              <div class="report-list">
                <label>单位名称：</label>
                <span>{{ item.name }}</span>
              </div>
              <div class="report-list">
                <label>纳税识别号：</label>
                <span>{{ item.identification }}</span>
              </div>
              <div class="report-list">
                <label>联系人：</label>
                <span>{{ item.contact }}</span>
              </div>
              <div class="report-list">
                <label>联系手机号：</label>
                <span>{{ item.contactMobile }}</span>
              </div>
              <div class="report-list">
                <label>收款账号：</label>
                <span>{{ item.proceedsAccount }}</span>
              </div>
              <div class="report-list">
                <label>收款银行：</label>
                <span>{{ showIdLabel(userList, item.bankList, 'bankName') }}</span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <div class="tabs-list-head" v-if="partyBList.length > 0">
          <span>乙方信息</span>
        </div>
        <el-col :sm="24" :lg="24" style="padding-left: 20px">
          <el-card class="update-log" v-for="(item,index) in partyBList" :key="index">
            <div class="report-form-list-content">
              <div class="report-list">
                <label>参与方类型：</label>
                <span>
                  <DictTag :dict-list="getDictList('ContractParticipationUserType')" :dict-key="item.participationUserType" />
                </span>
              </div>
              <div class="report-list">
                <label>单位名称：</label>
                <span>{{ item.name }}</span>
              </div>
              <div class="report-list">
                <label>纳税识别号：</label>
                <span>{{ item.identification }}</span>
              </div>
              <div class="report-list">
                <label>联系人：</label>
                <span>{{ item.contact }}</span>
              </div>
              <div class="report-list">
                <label>联系手机号：</label>
                <span>{{ item.contactMobile }}</span>
              </div>
              <div class="report-list">
                <label>收款账号：</label>
                <span>{{ item.proceedsAccount }}</span>
              </div>
              <div class="report-list">
                <label>收款银行：</label>
                <span>{{ showIdLabel(userList, item.bankList, 'bankName') }}</span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <div class="tabs-list-head" v-if="partyCList.length > 0">
          <span>第三方信息</span>
        </div>
        <el-col :sm="24" :lg="24" style="padding-left: 20px">
          <el-card class="update-log" v-for="(item,index) in partyCList" :key="index">
            <div class="report-form-list-content">
              <div class="report-list">
                <label>参与方类型：</label>
                <span>
                  <DictTag :dict-list="getDictList('ContractParticipationUserType')" :dict-key="item.participationUserType" />
                </span>
              </div>
              <div class="report-list">
                <label>单位名称：</label>
                <span>{{ item.name }}</span>
              </div>
              <div class="report-list">
                <label>纳税识别号：</label>
                <span>{{ item.identification }}</span>
              </div>
              <div class="report-list">
                <label>联系人：</label>
                <span>{{ item.contact }}</span>
              </div>
              <div class="report-list">
                <label>联系手机号：</label>
                <span>{{ item.contactMobile }}</span>
              </div>
              <div class="report-list">
                <label>收款账号：</label>
                <span>{{ item.proceedsAccount }}</span>
              </div>
              <div class="report-list">
                <label>收款银行：</label>
                <span>{{ showIdLabel(userList, item.bankList, 'bankName') }}</span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <div class="tabs-list-head tabs-list-head-1">
          <span>合同信息</span>
        </div>
        <el-col :sm="24" :lg="24" style="padding-left: 20px">
          <div class="report-list">
            <label>合同模版：</label>
            <span>{{ showIdLabel(contractTemplateList, obj.contractTemplateId, 'name') }}</span>
          </div>
          <div class="report-list">
            <label>合同内容：</label>
          </div>
          <div class="report-list">
            <p v-html="obj.content"></p>
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="tabs-list" v-show="activeName === '2'" v-if="obj.needInspection === '1'">
      <el-row :gutter="20">
        <el-col :sm="24" :lg="24" style="padding-left: 20px">
          <el-card class="update-log" v-for="(item,index) in inspectionsList" :key="index">
            <div class="report-form-list-content-1">
              <div class="report-list">
                <label>验收负责人：</label>
                <span>{{ showIdLabel(userList, item.principalUserId, 'name') }}</span>
              </div>
            </div>
            <div class="report-form-list-content-1">
              <div class="report-list">
                <label>验收内容：</label>
                <span>{{ item.content }}</span>
              </div>
            </div>
            <div class="report-form-list-content-1">
              <div class="report-list">
                <label>备注：</label>
                <span>{{ item.remark }}</span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
    <div class="tabs-list" v-show="activeName === '3'">
      <el-row :gutter="20" v-if="payeesList.length > 0">
        <div class="tabs-list-head">
          <span>收款计划</span>
        </div>
        <el-col :sm="24" :lg="24" style="padding-left: 20px">
          <el-card class="update-log" v-for="(item,index) in payeesList" :key="index">
            <div class="report-form-list-content">
              <div class="report-list">
                <label>收款批次：</label>
                <span>{{ item.batchNo }}</span>
              </div>
              <div class="report-list">
                <label>收款条件：</label>
                <span>{{ item.condition }}</span>
              </div>
              <div class="report-list">
                <label>约定收款金额：</label>
                <span>￥{{ item.amount }}</span>
              </div>
              <div class="report-list">
                <label>金额占比：</label>
                <span>{{ item.amountRate }}%</span>
              </div>
              <div class="report-list">
                <label>预计收款日期：</label>
                <span>{{ item.payTime?.substring(0,10) }}</span>
              </div>
              <div class="report-list">
                <label>付款单位：</label>
                <span>{{ item.payCompany }}</span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
      <el-divider />
      <el-row :gutter="20">
        <div class="tabs-list-head" v-if="paysList.length > 0">
          <span>付款计划</span>
        </div>
        <el-col :sm="24" :lg="24" style="padding-left: 20px">
          <el-card class="update-log" v-for="(item,index) in paysList" :key="index">
            <div class="report-form-list-content">
              <div class="report-list">
                <label>付款批次：</label>
                <span>{{ item.batchNo }}</span>
              </div>
              <div class="report-list">
                <label>付款条件：</label>
                <span>{{ item.condition }}</span>
              </div>
              <div class="report-list">
                <label>约定支付金额：</label>
                <span>￥{{ item.amount }}</span>
              </div>
              <div class="report-list">
                <label>金额占比：</label>
                <span>{{ item.amountRate }}%</span>
              </div>
              <div class="report-list">
                <label>预计收款日期：</label>
                <span>{{ item.payeeTime }}</span>
              </div>
              <div class="report-list">
                <label>收款单位：</label>
                <span>{{ item.payeeCompany }}</span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
    <div class="tabs-list" v-show="activeName === '4'">
      <!-- 附件信息 -->
      <el-table :data="oFiles"
                style="width: 100%; margin-top: 20px"
                row-key="id"
                border
                default-expand-all>
        <el-table-column type="selection" width="55" />
        <el-table-column label="附件类型">
          <template v-slot="{row}">
            {{ showIdLabel(contractFiletypeList, row.type, 'name') }}
          </template>
        </el-table-column>
        <el-table-column prop="original" label="附件名称" />
        <el-table-column align="center" label="操作" width="160px" fixed="right">
          <template #default="scope">
            <el-button type="primary" link @click="downloadFile1(scope.row)">下载</el-button>
            <el-button type="primary" link @click="viewFile1(scope.row)">预览</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog v-model="attachmentVisible" title="附件预览" width="70%" top="2vh">
      <attachmentPreview :attachmentId="previewAttachmentId" />
      <div style="text-align:right;">
        <el-button type="danger" @click="attachmentVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import {ref, watch} from 'vue'
import {doDownload, doGet} from "@/config/http";
import {svc, showIdLabel, getListPageSize, getEnumList, getDictList} from "@/config/common";

import attachmentPreview from '@/views/components/base/attachmentPreview.vue'

import { useRoute, useRouter } from 'vue-router'
import initListStore from '@/store/modules/initList'

const props = defineProps({
  contractInitialId: {
    required: true,
    type: String,
  }
})

let route = useRoute()
let router = useRouter()

let activeName = ref('1')
let previewAttachmentId = ref('')
let attachmentVisible = ref(false)
let oper = ref('')
let id = ref('')

let obj = ref({
  amount: '', // 合同金额
  code: '', // 合同编码
  content: '', // 合同内容
  contractComplete: '', // 	合同终止时间
  contractLaunch: '', // 合同起止时间
  contractSignerId: '', // 合同签订人ID
  contractTemplateId: '', // 合同模板ID
  departmentId: '', // 负责部门ID
  id: '', //	主键ID
  name: '', // 合同名称
  needInspection: '', // 是否需要验收
  contractSignerDepartmentId: '', // 合同签订部门ID
  state: '', // 合同状态
  type: '', // 合同类型
  userId: '', // 负责人用户ID
})

let oFiles = ref([])
let inspectionsList = ref([]) // 验收条件
let payeesList = ref([]) // 初始合同收款信息
let paysList = ref([]) // 初始合同付款信息
// let participationsList = ref([]) // 参与方信息

let partyAList = ref([]) // 甲方
let partyBList = ref([]) // 乙方
let partyCList = ref([]) // 第三方

let contractTypeList = initListStore().contractTypeList // 合同类型
// let contractCooperationList = ref([]) // 往来单位
// let contractFeetypeList = ref([]) // 合同费用类型
let contractTemplateList = initListStore().contractTemplateList // 合同模版
// let bankList = ref([]) // 银行
let userList = initListStore().userList // 用户
// let governmentList = ref([]) //  政府单位
// let organizationList = ref([]) // 部门
let contractSignerList = initListStore().contractSignerList // 签订人
let contractFiletypeList = initListStore().contractFiletypeList // 合同附件类型

// const initList = async () => {
//   let p1 = doGet(svc, '/contract-type/page', {pageSize: getListPageSize()}, false) // 合同类型
//   let p2 = doGet(svc, '/contract-cooperation/page', {pageSize: getListPageSize()}, false) // 往来单位
//   let p3 = doGet(svc, '/contract-feetype/page', {pageSize: getListPageSize()}, false) // 合同费用类型
//   let p4 = doGet(svc, '/contract-template/page', {pageSize: getListPageSize()}, false) // 合同模版
//   let p5 = doGet(svc, '/bank/page', {pageSize: getListPageSize()}, false) // 银行
//   let p6 = doGet(svc, '/user/page', {pageSize: getListPageSize()}, false) // 用户
//   let p7 = doGet(svc, '/government/page', {pageSize: getListPageSize()}, false) //  政府单位
//   let p8 = doGet(svc, '/organization/page', {pageSize: getListPageSize()}, false) // 部门
//   let p9 = doGet(svc, '/contract-signer/page', {pageSize: getListPageSize()}, false) // 签订人
//   let p10 = doGet(svc, '/contract-filetype/page', {pageSize: getListPageSize()}, false) // 合同附件类型
//   let resArray = await Promise.all([p1, p2, p3, p4, p5, p6, p7, p8, p9, p10])
//   contractTypeList.value = resArray[0]['records']
//   contractCooperationList.value = resArray[1]['records']
//   contractFeetypeList.value = resArray[2]['records']
//   contractTemplateList.value = resArray[3]['records']
//   bankList.value = resArray[4]['records']
//   userList.value = resArray[5]['records']
//   governmentList.value = resArray[6]['records']
//   organizationList.value = resArray[7]['records']
//   contractSignerList.value = resArray[8]['records']
//   contractFiletypeList.value = resArray[9]['records']
// }

const initObj = async (val) =>  {
  oper.value = route.query.oper
  id.value = val
  if (id.value) {
    obj.value = await doGet(svc, `/contract-initial/${id.value}`, {}, true)
    // 验收条件
    let inspectionsList1 = await doGet(svc, '/contract-initial-inspection/listByField', {field: 'contractInitialId', values: id.value}, false)
    // 初始合同收款信息
    let payeesList1 = await doGet(svc, '/contract-initial-payee/listByField', {field: 'contractInitialId', values: id.value}, false)
    // 初始合同付款信息
    let paysList1 = await doGet(svc, '/contract-initial-pay/listByField', {field: 'contractInitialId', values: id.value}, false)
    // 参与方信息
    let participationsList1 = await doGet(svc, '/contract-initial-participation/listByField', {field: 'contractInitialId', values: id.value}, false)
    // 附件信息
    let oFiles1 = await doGet(svc, '/contract-initial-file/listByField', {field: 'contractInitialId', values: id.value}, false)
    oFiles.value = oFiles1.map(item => {
      item.original = item.attachmentName
      item.fileType = item.attachmentType
      return item
    })

    if (inspectionsList1.length > 0) {
      inspectionsList.value = inspectionsList1
    }

    if (payeesList1.length > 0) {
      payeesList.value = payeesList1
    }

    if (paysList1.length > 0) {
      paysList.value = paysList1
    }
    if (participationsList1.length > 0) {
      partyAList.value = participationsList1.filter(item => item.participationType === '0')
      partyBList.value = participationsList1.filter(item => item.participationType === '1')
      partyCList.value = participationsList1.filter(item => item.participationType === '2')
    }
  }
}

watch(() => props.contractInitialId, async val => {
  await initObj(val)
  // await initList(val)
},{immediate:true})

const downloadFile1 = (row) => {
  doDownload(svc, '/customAttachment/download/' + row.attachmentId, {})
}
const viewFile1 = (row) => {
  attachmentVisible.value = true
  setTimeout(() => {
    previewAttachmentId.value = row.attachmentId
  },100)
}
</script>

<style scoped>
.report-form-content {
  padding: 20px;
}
.tabs-list-head-2 {
  padding-left: 0 !important;
}
.tabs-list-head-2 span {
  font-size: 22px !important;
  color: #1ac4ad !important;
}
.tabs-list-head {
  padding-left: 20px;
  display: flex;
  align-items: center;
}
.tabs-list-head span {
  font-size: 18px;
  color: #303133;
  font-weight: bold;
}
.tabs-list-head-1 {
  margin-bottom: 30px;
}
.tabs-list-type {
  display: flex;
  align-items: center;
  justify-content: center;
}
.update-log {
  width: 100%;
  margin: 30px 0;
}
.update-log-1 {
  margin-top: 0;
}
.report-list {
  /* width: 100%; */
  width: 33%;
  margin: 15px;
  display: flex;
  align-items: center;
  /* justify-content: space-between; */
}
.report-list-a {

}
.report-list-a p {
  margin-left: 55px;
}
.report-list span:nth-child(1) {
  display: inline-block;
  width: 120px;
  text-align: right;
}
.steps {
  margin: 20px 0
}
</style>
